home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / RandomTest.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  7.4 KB  |  279 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1991-93 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Date: Tuesday, June 2, 1992 11:53:42
  5.   Revision comments are at the end of this file.
  6.   ---
  7.   TRandom is a stackbased utility class for random number generation.
  8.   TRandomTest.cp contains the test functions for the TRandom class.
  9.   _________________________________________________________________________________________________________ */
  10.  
  11. // HEADERS
  12. #ifndef _RANDOM_
  13. #include "Random.h"
  14. #endif
  15.  
  16. // GLOBALS
  17. void CalcTime(const char* string);                // calculate time function
  18. unsigned long timeStart;                        // global timing
  19.  
  20.  
  21. // Test the TRandom class inside out.
  22.  
  23. void main(void)
  24. {
  25.     const short cARRAYSIZE = 80;                // 200 is a nice size
  26.     const long cBIGNUM = 1000;                    //cBIGNUM= 1000000 takes about 40 sec
  27.     unsigned short val;
  28.     long x;
  29.     unsigned short array1[cARRAYSIZE],
  30.      array2[cARRAYSIZE];
  31.  
  32.     InitGraf((Ptr) & qd.thePort);                // Initialize grafport!
  33.  
  34.     //    First initial quick test of the toolbox value
  35.     qd.randSeed = TickCount();
  36.     long xx = Random();
  37.     cerr << "Normal Toolbox Random     = " << xx << '\n';
  38.  
  39.  
  40.     //    1. Create an instance of each TRandom class
  41.     TRandom aRandom;
  42.     val = aRandom.Next();
  43.     cerr << "Toolbox aRandom.Next    = " << val << "\n";
  44.  
  45.     aRandom.SetRandomGenerator(TRandom::kQUICK);
  46.     val = aRandom.Next();
  47.     cerr << "Quick aRandom.Next        = " << val << '\n';
  48.  
  49.     aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
  50.     val = aRandom.Next();
  51.     cerr << "Shuffle aRandom.Next    = " << val << '\n';
  52.  
  53.     aRandom.SetRandomGenerator(TRandom::kPM);
  54.     val = aRandom.Next();
  55.     cerr << "PM aRandom.Next            = " << val << '\n';
  56.  
  57.     //     2. Efficiency test, docBIGNUMtimes random numbers and return the time
  58.     cerr << "Doing " << cBIGNUM << " random numbers of each class...this will take some time\n";
  59.  
  60.     // Toolbox Random    
  61.     aRandom.SetRandomGenerator(TRandom::kMACOS);
  62.     GetDateTime(&timeStart);
  63.     for (x = 0; x <= cBIGNUM; x++)
  64.     {
  65.         val = aRandom.Next();
  66. #if DEBUG
  67.         cerr << "Toolbox Random " << x << " = " << val << '\n';
  68. #endif
  69.  
  70.     }
  71.     CalcTime("Toolbox");
  72.  
  73.     // Quick Random    
  74.     aRandom.SetRandomGenerator(TRandom::kQUICK);
  75.     GetDateTime(&timeStart);
  76.     for (x = 0; x <= cBIGNUM; x++)
  77.     {
  78.         val = aRandom.Next();
  79. #if DEBUG
  80.         cerr << "Quick Random " << x << " = " << val << '\n';
  81. #endif
  82.  
  83.     }
  84.     CalcTime("Quick");
  85.  
  86.     // Shuffle Random    
  87.     aRandom.SetRandomGenerator(TRandom::kSHUFFLE);
  88.     GetDateTime(&timeStart);
  89.     for (x = 0; x <= cBIGNUM; x++)
  90.     {
  91.         val = aRandom.Next();
  92. #if DEBUG
  93.         cerr << "Shuffle Random " << x << " = " << val << '\n';
  94. #endif
  95.  
  96.     }
  97.     CalcTime("Shuffle");
  98.  
  99.     // PM Random
  100.     aRandom.SetRandomGenerator(TRandom::kPM);
  101.     GetDateTime(&timeStart);
  102.     for (x = 0; x <= cBIGNUM; x++)
  103.     {
  104.         val = aRandom.Next();
  105. #if DEBUG
  106.         cerr << "PM Random " << x << " = " << val << '\n';
  107. #endif
  108.  
  109.     }
  110.     CalcTime("PM");
  111.  
  112.     //    3. Performance test, create twice the random numbers with the same
  113.     //    original seed, and compare the results, signal if they differ
  114.  
  115.     long definedSeed;
  116.  
  117.     cerr << "Performance test, create from same seed twice and compare...\n";
  118.  
  119.     //    MACOS Random Performance test
  120.     cerr << "MacOS Random...";
  121.     aRandom.SetRandomGenerator(TRandom::kMACOS);
  122.  
  123.     aRandom.ShuffleSeed();                        // get new seed
  124.     definedSeed = aRandom.GetSeedValue();        // store it
  125.  
  126.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  127.  
  128.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  129.         array1[x] = aRandom.Next();
  130.  
  131.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  132.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  133.         array2[x] = aRandom.Next();
  134.  
  135.     for (x = 0; x < cARRAYSIZE; x++)
  136.     {                                            // compare
  137.         if (array1[x] != array2[x])
  138.         {
  139.             cerr << "problems with Toolbox values....\n";
  140.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  141.         }
  142.         else
  143.             cerr << ".";
  144.     }
  145.     cerr << "\n";
  146.  
  147.     //    Quick Random Performance test
  148.     cerr << "Quick Random...";
  149.     aRandom.SetRandomGenerator(TRandom::kQUICK);
  150.  
  151.     aRandom.ShuffleSeed();                        // get new seed
  152.     definedSeed = aRandom.GetSeedValue();        // store it
  153.  
  154.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  155.  
  156.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  157.         array1[x] = aRandom.Next();
  158.  
  159.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  160.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  161.         array2[x] = aRandom.Next();
  162.  
  163.     for (x = 0; x < cARRAYSIZE; x++)
  164.     {                                            // compare
  165.         if (array1[x] != array2[x])
  166.         {
  167.             cerr << "problems with Quick values....\n";
  168.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  169.         }
  170.         else
  171.             cerr << ".";
  172.     }
  173.     cerr << "\n";
  174.  
  175.     //    Shuffle Random Performance test
  176.     cerr << "Shuffle Random...";
  177.     TRandom rand1(TRandom::kSHUFFLE);
  178.     TRandom rand2(TRandom::kSHUFFLE);
  179.  
  180.     aRandom.ShuffleSeed();                        // get new seed
  181.     definedSeed = aRandom.GetSeedValue();        // store it
  182.  
  183.     rand1.SetSeedValue(definedSeed);            // back to first seed
  184.  
  185.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  186.         array1[x] = rand1.Next();
  187.  
  188.     rand2.SetSeedValue(definedSeed);            // back to first seed
  189.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  190.         array2[x] = rand2.Next();
  191.  
  192.     for (x = 0; x < cARRAYSIZE; x++)
  193.     {                                            // compare
  194.         if (array1[x] != array2[x])
  195.         {
  196.             cerr << "problems with Quick values....\n";
  197.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  198.         }
  199.         else
  200.             cerr << ".";
  201.     }
  202.     cerr << "\n";
  203.  
  204.     //    Park-Miller
  205.     cerr << "PM Random...";
  206.     aRandom.SetRandomGenerator(TRandom::kPM);
  207.  
  208.     aRandom.ShuffleSeed();                        // get new seed
  209.     definedSeed = aRandom.GetSeedValue();        // store it
  210.  
  211.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  212.  
  213.     for (x = 0; x < cARRAYSIZE; x++)            // fill array1
  214.         array1[x] = aRandom.Next();
  215.  
  216.     aRandom.SetSeedValue(definedSeed);            // back to first seed
  217.  
  218.     for (x = 0; x < cARRAYSIZE; x++)            // fill array2
  219.         array2[x] = aRandom.Next();
  220.  
  221.     for (x = 0; x < cARRAYSIZE; x++)
  222.     {                                            // compare
  223.         if (array1[x] != array2[x])
  224.         {
  225.             cerr << "problems with PM values....\n";
  226.             cerr << "x = " << x << ",array1 = " << array1[x] << ", array2 = " << array2[x] << '\n';
  227.         }
  228.         else
  229.             cerr << ".";
  230.     }
  231.     cerr << "\n";
  232.  
  233.     //    4. Test mutator class reference passing (syntax check)
  234.     aRandom.SetRandomGenerator(TRandom::kMACOS).ShuffleSeed();
  235.  
  236.     //    5. Test of copy constructor
  237.     cerr << "\nTest of copy constructor\n";
  238.  
  239.     TRandom bRandom(aRandom);
  240.     cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
  241.     cerr << "Seed from bRandom = " << bRandom.GetSeedValue() << "\n";
  242.     cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
  243.     cerr << "Rand val from bRandom = " << bRandom.Next() << "\n";
  244.  
  245.  
  246.     //     6. Test of assignment operator overload
  247.     cerr << "\nTest of assignment operator overload\n";
  248.  
  249.     TRandom cRandom = aRandom;
  250.     cerr << "Seed from aRandom = " << aRandom.GetSeedValue() << "\n";
  251.     cerr << "Seed from cRandom = " << cRandom.GetSeedValue() << "\n";
  252.     cerr << "Rand val from aRandom = " << aRandom.Next() << "\n";
  253.     cerr << "Rand val from cRandom = " << cRandom.Next() << "\n";
  254.  
  255.     cerr << "The end of the TRandom Test!\n";
  256. }
  257.  
  258.  
  259. void CalcTime(const char* string)
  260. // Calculate Time.
  261. {
  262.     unsigned long totalTime, timeEnd;
  263.  
  264.     ::GetDateTime(&timeEnd);
  265.     totalTime = timeEnd - timeStart;
  266.     cerr << "Time for \t" << string << "\t= " << totalTime << '\n';
  267. }
  268.  
  269.  
  270. // _________________________________________________________________________________________________________ //
  271.  
  272.  
  273. /*    Change History (most recent last):
  274.   No        Init.    Date        Comment
  275.   1            khs        6/2/92        New file
  276.   2            khs        1/7/93        Cleanup
  277. */
  278.  
  279.